﻿Imports TyranntEngine
Imports System.IO

Public Class selectRaceForm

    Private _token As String
    Private WithEvents _client As New TyranntOnlineCharacterService.TyranntCharacterServiceClient
    Private _char As New PlayerCharacter
    Private _currentRace As New CharacterRace
    Private _raceTable As New Hashtable
    Private _portraitNumber As Integer = -1

    Public Sub Init(ByVal token As String, ByVal charXML As String)
        _token = token
        _char.LoadXML(charXML)
        _client.GetRacesAsync(_token)
    End Sub

    Private Sub _client_GetRacesCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetRacesCompletedEventArgs) Handles _client.GetRacesCompleted
        If e.Result.Contains("[#]") Then
            FatalError(e.Result)
        End If
        raceComboBox.Items.Clear()
        Dim sr As New StringReader(e.Result)
        Dim racesXML As XElement = XElement.Load(sr)
        Dim races = racesXML...<race>
        For Each aRace In races
            Dim raceID As String = aRace.@id
            Dim raceName As String = aRace.<name>.Value
            _raceTable(raceName) = raceID
            raceComboBox.Items.Add(raceName)
        Next
        RefreshForm()
    End Sub

    Private Sub selectRaceForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = _char.Name + " Select your Sex and Race"
        CheckValid()
    End Sub

    Private Sub FatalError(ByVal message As String)
        MessageBox.Show(message, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End
    End Sub

    Private Sub RefreshForm()
        If _currentRace.Description <> "" Then
            raceDescriptionTextBox.Text = _currentRace.Description + vbCrLf
            Dim extra As String = ""
            If _currentRace.StrengthModifier <> 0 Then
                extra += _currentRace.StrengthModifier.ToString + " Strength "
            End If
            If _currentRace.DexterityModifier <> 0 Then
                extra += _currentRace.DexterityModifier.ToString + " Dexterity "
            End If
            If _currentRace.IntelligenceModifier <> 0 Then
                extra += _currentRace.IntelligenceModifier.ToString + " Intellegence"
            End If
            If extra <> "" Then
                raceDescriptionTextBox.Text += _currentRace.Name + " Get " + extra
            End If
            If maleRadioButton.Checked = True Then
                raceDescPictureBox.Image = Image.FromFile("./media/" + _currentRace.MaleImage)
                racePortrait1PictureBox.Image = Image.FromFile("./media/" + _currentRace.MalePortrait(0))
                racePortrait2PictureBox.Image = Image.FromFile("./media/" + _currentRace.MalePortrait(1))
                racePortrait3PictureBox.Image = Image.FromFile("./media/" + _currentRace.MalePortrait(2))
                racePortrait4PictureBox.Image = Image.FromFile("./media/" + _currentRace.MalePortrait(3))
                racePortrait5PictureBox.Image = Image.FromFile("./media/" + _currentRace.MalePortrait(4))
                racePortrait6PictureBox.Image = Image.FromFile("./media/" + _currentRace.MalePortrait(5))
            ElseIf femaleRadioButton.Checked = True Then
                raceDescPictureBox.Image = Image.FromFile("./media/" + _currentRace.FemaleImage)
                racePortrait1PictureBox.Image = Image.FromFile("./media/" + _currentRace.FemalePortrait(0))
                racePortrait2PictureBox.Image = Image.FromFile("./media/" + _currentRace.FemalePortrait(1))
                racePortrait3PictureBox.Image = Image.FromFile("./media/" + _currentRace.FemalePortrait(2))
                racePortrait4PictureBox.Image = Image.FromFile("./media/" + _currentRace.FemalePortrait(3))
                racePortrait5PictureBox.Image = Image.FromFile("./media/" + _currentRace.FemalePortrait(4))
                racePortrait6PictureBox.Image = Image.FromFile("./media/" + _currentRace.FemalePortrait(5))
            Else
                raceDescPictureBox.Image = Image.FromFile("./media/" + _currentRace.UnknownSexImage)
                racePortrait1PictureBox.Image = Nothing
                racePortrait2PictureBox.Image = Nothing
                racePortrait3PictureBox.Image = Nothing
                racePortrait4PictureBox.Image = Nothing
                racePortrait5PictureBox.Image = Nothing
                racePortrait6PictureBox.Image = Nothing
            End If
            If _portraitNumber = 1 Then
                racePortrait1PictureBox.BorderStyle = BorderStyle.Fixed3D
            Else
                racePortrait1PictureBox.BorderStyle = BorderStyle.FixedSingle
            End If
            If _portraitNumber = 2 Then
                racePortrait2PictureBox.BorderStyle = BorderStyle.Fixed3D
            Else
                racePortrait2PictureBox.BorderStyle = BorderStyle.FixedSingle
            End If
            If _portraitNumber = 3 Then
                racePortrait3PictureBox.BorderStyle = BorderStyle.Fixed3D
            Else
                racePortrait3PictureBox.BorderStyle = BorderStyle.FixedSingle
            End If
            If _portraitNumber = 4 Then
                racePortrait4PictureBox.BorderStyle = BorderStyle.Fixed3D
            Else
                racePortrait4PictureBox.BorderStyle = BorderStyle.FixedSingle
            End If
            If _portraitNumber = 5 Then
                racePortrait5PictureBox.BorderStyle = BorderStyle.Fixed3D
            Else
                racePortrait5PictureBox.BorderStyle = BorderStyle.FixedSingle
            End If
            If _portraitNumber = 6 Then
                racePortrait6PictureBox.BorderStyle = BorderStyle.Fixed3D
            Else
                racePortrait6PictureBox.BorderStyle = BorderStyle.FixedSingle
            End If
        Else
            raceDescriptionTextBox.Text = ""
            racePortrait1PictureBox.Image = Nothing
            racePortrait2PictureBox.Image = Nothing
            racePortrait3PictureBox.Image = Nothing
            racePortrait4PictureBox.Image = Nothing
            racePortrait5PictureBox.Image = Nothing
            racePortrait6PictureBox.Image = Nothing
            raceDescPictureBox.Image = Image.FromFile("./media/csd.png")
            maleRadioButton.Checked = False
            femaleRadioButton.Checked = False
        End If
        CheckValid()
    End Sub

    Private Sub raceComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles raceComboBox.SelectedIndexChanged
        If raceComboBox.Text <> "" Then
            _client.GetRaceAsync(_token, Integer.Parse(CStr(_raceTable(raceComboBox.Text))))
        End If
    End Sub

    Private Sub _client_GetRaceCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetRaceCompletedEventArgs) Handles _client.GetRaceCompleted
        If e.Result.Contains("[#]") Then
            FatalError(e.Result)
        End If
        _currentRace.LoadXML(e.Result)
        _char.RaceID = _currentRace.RaceID
        RefreshForm()
    End Sub

    Private Sub femaleRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles femaleRadioButton.CheckedChanged
        _char.IsMale = False
        RefreshForm()
    End Sub

    Private Sub maleRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maleRadioButton.CheckedChanged
        _char.IsMale = True
        RefreshForm()
    End Sub

    Private Sub racePortrait1PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles racePortrait1PictureBox.Click
        If maleRadioButton.Checked = True Then
            _char.RacePortraitName = _currentRace.MalePortrait(0)
        Else
            _char.RacePortraitName = _currentRace.FemalePortrait(0)
        End If
        _portraitNumber = 1
        RefreshForm()
    End Sub

    Private Sub racePortrait2PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles racePortrait2PictureBox.Click
        If _char.IsMale = True Then
            _char.RacePortraitName = _currentRace.MalePortrait(1)
        Else
            _char.RacePortraitName = _currentRace.FemalePortrait(1)
        End If
        _portraitNumber = 2
        RefreshForm()
    End Sub

    Private Sub racePortrait3PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles racePortrait3PictureBox.Click
        If _char.IsMale = True Then
            _char.RacePortraitName = _currentRace.MalePortrait(2)
        Else
            _char.RacePortraitName = _currentRace.FemalePortrait(2)
        End If
        _portraitNumber = 3
        RefreshForm()
    End Sub

    Private Sub racePortrait4PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles racePortrait4PictureBox.Click
        If _char.IsMale = True Then
            _char.RacePortraitName = _currentRace.MalePortrait(3)
        Else
            _char.RacePortraitName = _currentRace.FemalePortrait(3)
        End If
        _portraitNumber = 4
        RefreshForm()
    End Sub

    Private Sub racePortrait5PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles racePortrait5PictureBox.Click
        If _char.IsMale = True Then
            _char.RacePortraitName = _currentRace.MalePortrait(4)
        Else
            _char.RacePortraitName = _currentRace.FemalePortrait(4)
        End If
        _portraitNumber = 5
        RefreshForm()
    End Sub

    Private Sub racePortrait6PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles racePortrait6PictureBox.Click
        If _char.IsMale = True Then
            _char.RacePortraitName = _currentRace.MalePortrait(5)
        Else
            _char.RacePortraitName = _currentRace.FemalePortrait(5)
        End If
        _portraitNumber = 6
        RefreshForm()
    End Sub

    Private Sub CheckValid()
        Dim valid As Boolean = True
        If _char.RaceID = -1 Then
            valid = False
        End If
        If maleRadioButton.Checked = False And femaleRadioButton.Checked = False Then
            valid = False
        End If
        If _char.RacePortraitName = "" Then
            valid = False
        End If
        If valid = False Then
            nextButton.Enabled = False
        Else
            nextButton.Enabled = True
        End If
    End Sub

    Private Sub cancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles canceledButton.Click
        canceledButton.Enabled = False
        nextButton.Enabled = False
        _client.DeleteCharacterAsync(_token, _char.ID)
    End Sub

    Private Sub _client_DeleteCharacterCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.DeleteCharacterCompletedEventArgs) Handles _client.DeleteCharacterCompleted
        If e.Result <> "" Then
            MessageBox.Show(e.Result, "Delete Character Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        Me.Close()
    End Sub

    Private Sub nextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextButton.Click
        nextButton.Enabled = False
        _client.SelectRaceAsync(_token, _char.ID, _char.RaceID, _char.IsMale, _char.RacePortraitName)
    End Sub

    Private Sub _client_SelectRaceCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.SelectRaceCompletedEventArgs) Handles _client.SelectRaceCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "Select Race Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If

        ' New character worked so now launch next form
        Me.Hide()
        Dim selectGuild As New selectGuildForm
        selectGuild.Init(_token, e.Result)
        selectGuild.ShowDialog()
        Me.Close()
    End Sub
End Class